import util from '@/libs/util';
import classroomComp from '../classroom/classroomlist.vue';

export default {
    components: { classroomComp},
    data () {
        return {
            columnsSchedule: [{
                title: '上课周期',
                key: 'classTime',
                render: (h, params) => {
                return h('Select', {
                    props: {
                        value: this.dataListSchedule[params.index].classTime,
                        'label-in-value': true
                    },
                    on: {
                        'on-change': (event) => {
                            var index = params.index;
                            var obj = this.dataListSchedule[index];
                            obj.classTime = event.value;
                            obj.classTimeName = event.label;
                        }
                    }
                },
                [
                    h('Option', {
                        props: {
                            value: '1'
                        }
                    }, '周一'),
                    h('Option', {
                        props: {
                            value: '2'
                        }
                    }, '周二'),
                    h('Option', {
                        props: {
                            value: '3'
                        }
                    }, '周三'),
                    h('Option', {
                        props: {
                            value: '4'
                        }
                    }, '周四'),
                    h('Option', {
                        props: {
                            value: '5'
                        }
                    }, '周五'),
                    h('Option', {
                        props: {
                            value: '6'
                        }
                    }, '周六'),
                    h('Option', {
                        props: {
                            value: '7'
                        }
                    }, '周日')
                ]);
                }
            }, {
                title: '教室',
                key: 'classRoom',
                render: (h, params) => {
                var that = this;
                return h('Input', {
                    props: {
                        icon: 'search',
                        readonly: true,
                        value: params.row.classRoom
                    },
                    on: {
                        'on-click': (event) => {
                            util.ajax.post('/base/classroom/list', util.toFormData(
                            )).then(res => {
                                if (res.data.code == 200) {
                                var data = res.data.data.list;
                                this.dataListClassRoom = data;
                                }
                             });
                            this.classRoomEntry = 1;// 因为选择教室的modal是公用的，所以要做一个入口标识
                            this.classRoomVisible = true;
                            this.currentDataListScheduleIndex = params.index;
                        }
                     }
                });
                }
            }, {
                title: '开始时间',
                key: 'startTime',
                render: (h, params) => {
                return h('TimePicker', {
                    props: {
                        format: 'HH:mm',
                        value: this.dataListSchedule[params.index].startTime
                    },
                    on: {
                        'on-change': (event) => {
                            this.dataListSchedule[params.index].startTime = event
                         }
                    }
                });
                }
            }, {
                title: '结束时间',
                key: 'endTime',
                render: (h, params) => {
                return h('TimePicker', {
                    props: {
                        format: 'HH:mm',
                        value: this.dataListSchedule[params.index].endTime
                    },
                    on: {
                        'on-change': (event) => {
                            this.dataListSchedule[params.index].endTime = event;
                        }
                    }
                });
                }
            }, {
                title: '操作',
                key: 'action',
                render: (h, params) => {
                return h('div', [
                    h('Button', {
                        props: {
                            icon: 'plus',
                            size: 'small'
                        },
                        style: {
                            marginRight: '5px'
                        },
                        on: {
                            'click': (event) => {
                                this.addRow(params.index, undefined);
                            }
                        }
                    }, '新增'),
                    h('Button', {
                        props: {
                            icon: 'minus',
                            size: 'small'
                        },
                        on: {
                            'click': (event) => {
                                this.delRow(params);
                            }
                        }
                    }, '删除')
                ]);
                }
            }],
            columns: [{
                width: 55,
                title: '#',
                type: 'selection'
            }, {
                title: '班级名称',
                key: 'name'
            }, {
                title: '负责教师',
                key: 'mainTeacherName'
            }, {
                title: '班级人数',
                key: 'people'
            }, {
                title: '课程安排',
                key: 'classSchedules',
                width: 180,
                render: function (h, params) {
                    let list = params.row.classSchedules;
                    if (typeof (list) === 'string') {
                        list = JSON.parse(list);
                    }
                    let list_h = [];
                    for (let i in list) {
                        let startTime = list[i].classStartTime;
                        let endTime = list[i].classEndTime;
                        if (startTime) {
                            startTime = startTime.substring(0, 2) + ':' + startTime.substring(2, 4);
                        }
                        if (endTime) {
                            endTime = endTime.substring(0, 2) + ':' + endTime.substring(2, 4);
                        }
                        let fun = h('div', util.convertNumToWeekOfDay(list[i].classType) + ' ' + startTime + '-' + endTime + ' ' + list[i].classRoomName);
                        list_h.push(fun);
                    }
                    return h('span', list_h);
                }
            }, {
                title: '操作',
                key: 'action',
                width: 200,
                align: 'center',
                render: (h, params) => {
                return h('div', [
                    h('Button', {
                        props: {
                            type: 'primary',
                            size: 'small'
                        },
                        style: {
                            marginRight: '5px'
                        },
                        on: {
                            click: () => {
                                this.tagCloseableFlag = false;
                                util.ajax.post('/base/class/detailsBySql', util.toFormData(
                                    {id: params.row.id}
                                )).then(res => { // 获得区域数据
                                    if (res.data.code == 200) {
                                        this.$refs['formData'].resetFields();
                                        var data = res.data.data;
                                        this.showEdit(data);
                                    }
                                });

                             }
                        }
                    }, '修改'),
                    h('Button', {
                        props: {
                            type: 'error',
                            size: 'small'
                        },
                        on: {
                            click: () => {
                                let obj = [];
                                obj.push(params.row.id);
                                this.doDel(obj);
                            }
                        }
                    }, '删除')
                ]);
                }
            }],
            dataList: [],
            addVisible: false, // 新增弹窗的显示和隐藏
            isRead: false, // 新增、编辑或查看时，数据是否可以修改
            formData: {
                name: '',
                people: '',
                mainTeacherId: '',
                teachCost: 0,
                classNo: 0,
                classTime: 0,
                classScheduleJson: ''
            },
            pageSize: 10, // 分页数据
            pageIndex: 1, // 分页数据
            total: 0,
            query: {name: ''},
            mainTeacherList: [],
            scheduleVisible: false,
            classRoomVisible: false,
            currentDataListScheduleIndex: '', // 当前课表数组对象焦点
            dataListSchedule: [],
            selection: [], // 选中行
            bathAddClassRoomScheduleVisible: false,
            bathAddClassRoomData: {
            classTime: []
        },
            classRoomEntry: '',
            weekData: [
            {id: 1, name: '周一'},
            {id: 2, name: '周二'},
            {id: 3, name: '周三'},
            {id: 4, name: '周四'},
            {id: 5, name: '周五'},
            {id: 6, name: '周六'},
            {id: 7, name: '周日'}
        ],
            tagCloseableFlag: true// 课表标签是否可以关闭
        };
    },
    mounted () {
        this.init();
    },
    methods: {
        init: function () { // 页面初始化
            util.ajax.post('/busine/tescher/info/list', util.toFormData(
                {id: 0}
            )).then(res => { // 获得老师
                if (res.data.code == 200) {
                    var data = res.data.data.list;
                    this.mainTeacherList = data;
                }
            });
            this.doQuery();
        },
        doQuery: function () {
            util.ajax.post('/base/class/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                name: this.query.name ? this.query.name : ''
            })).then(res => {
                if (res.data.data) {
                    this.dataList = res.data.data.list;
                    this.total = res.data.data.total;
                }
            });
        },
        refresh () {
            this.query = {
                name: ''
            };
            this.init();
        },
        showEdit:function(obj){
            this.dataListSchedule = [];
            let classSchedules = obj.classSchedules;
            if (classSchedules) {
                for (let i in classSchedules) {
                    let item = {};
                    item.id = classSchedules[i].id;
                    item.classRoom = classSchedules[i].classRoomName;
                    let classType = classSchedules[i].classType;
                    item.classTime = classType;
                    item.classTimeName = util.convertNumToWeekOfDay(classType);
                    let startTime = classSchedules[i].classStartTime;
                    let endTime = classSchedules[i].classEndTime;
                    if (startTime != null && startTime != '') {
                        item.startTime = startTime.substring(0, 2) + ':' + startTime.substring(2, 4);
                    }
                    if (endTime != null && endTime != '') {
                        item.endTime = endTime.substring(0, 2) + ':' + endTime.substring(2, 4);
                    }
                    this.dataListSchedule.push(item);
                }
            }
            this.formData = obj;
            this.tagCloseableFlag = false;
            this.addVisible = true;
        },
        showAdd: function (obj) {
            this.formData = {
                name: '',
                people: '',
                mainTeacherId: '',
                teachCost: 0,
                classNo: 0,
                classTime: 0,
                classScheduleJson: ''
            };
            this.dataListSchedule = [];
            this.tagCloseableFlag = true;
            this.addVisible = true;
        },
        doDel: function (ids) {
            if (ids) {
                this.$Modal.confirm({
                    title: '提示',
                    content: '确认删除？',
                    onOk: () => {
                    util.ajax.post('/base/class/delete', util.toFormData({ids: ids.join('@')})).then(res => {
                    if (res.data.code == 200) {
                    if (res.data.message == 'SUCCESS') {
                        this.$Message.success('删除成功！');
                        this.pageIndex = 1;
                        this.init();
                    } else {
                        this.$Message.warning(res.data.message || '未知错误！');
                    }
                }
            });
            },
                onCancel: () => {

                }
            });
            } else {
                this.$Modal.error({content: '请选择删除项!'});
            }
        },
        doSave: function () {
            let formData = this.formData;
            this.formData.classScheduleJson = this.dataListSchedule.length <= 0 ? '' : JSON.stringify(this.dataListSchedule);
            delete this.formData.classSchedules;
            util.ajax.post('/base/class/save', util.toFormData(formData)).then(res => {
                if (res.data.code == 200) {
                    if (res.data.message == 'SUCCESS') {
                        this.$Message.success('保存成功！');
                        this.addVisible = false;
                        this.pageIndex = 1;
                        this.init();
                    } else {
                        this.$Message.warning(res.data.message || '未知错误！');
                        this.addVisible = false;
                    }
                    this.formData = {};
                }
            });
        },
        showClassSchedule: function () {
            this.scheduleVisible = true;
        },
        addRow: function (index, obj) {
            if (obj == undefined) {
                obj = {
                    'tempid': index,
                    'id': '',
                    'classRoomId': '',
                    'classTime': '',
                    'classTimeName': '',
                    'classRoom': '',
                    'startTime': '',
                    'endTime': '',
                    'action': ''
                };
            }
            this.dataListSchedule.push(obj);
        },
        delRow: function (params) {
            var index = params.index;
            this.dataListSchedule.splice(index, 1);
        },
        whenCloseTag: function (event, name) {
            var id = name.id;
            var tempid = name.tempid;
            var delIndex = '';
            var list = this.dataListSchedule;
            for (let i in list) {
                if (list[i].id != null && list[i].id != '' && list[i].id != undefined) {
                    if (id == list[i].id) {
                        delIndex = i;
                        break;
                    }
                } else if (list[i].tempid != null && list[i].tempid != '' && list[i].tempid != undefined) {
                    if (tempid == list[i].tempid) {
                        delIndex = i;
                        break;
                    }
                }
            }
            this.dataListSchedule.splice(delIndex, 1);
        },
        doDelBath: function (selection) { // 批量删除
            let list = this.selection;
            if (list.length == 0) {
                this.$Message.error('请选择删除项目');
                return;
            }
            var ids = [];
            for (let i in list) {
                ids.push(list[i].id);
            }
            this.doDel(ids);
        },
        whenSelectRow: function (selection) {
            this.selection = selection;
        },
        showClassRoomSelecter: function () {
            util.ajax.post('/base/classroom/list', util.toFormData(
            )).then(res => {
                if (res.data.code == 200) {
                var data = res.data.data.list;
                this.dataListClassRoom = data;
            }
        });
            this.classRoomVisible = true;
            this.classRoomEntry = 2;
        },
        bathAddClassRoomScheduleClose: function () {
            this.bathAddClassRoomScheduleVisible = false;
        },
        bathAddClassRoomSchedule: function () {
            this.bathAddClassRoomScheduleVisible = true;
            this.bathAddClassRoomData = {
                classTime: []
            };
        },
        bathAddClassRoomConfirm: function () {
            let data = this.bathAddClassRoomData;
            let classTimes = data.classTime;
            if (data.classTime == null || data.classTime == '' || data.classTime == undefined || data.classTime.length == 0) {
                this.$Modal.warning({content: '请选择上课周期'});
                return;
            }
            let first_index = this.dataListSchedule.length;
            for (let i in classTimes) {
                let d = classTimes[i];
                let index = first_index;
                let classTimeName = '';
                switch (d + '') {
                    case '1':
                        classTimeName = '周一';
                        break;
                    case '2':
                        classTimeName = '周二';
                        break;
                    case '3':
                        classTimeName = '周三';
                        break;
                    case '4':
                        classTimeName = '周四';
                        break;
                    case '5':
                        classTimeName = '周五';
                        break;
                    case '6':
                        classTimeName = '周六';
                        break;
                    case '7':
                        classTimeName = '周日';
                        break;
                    default:
                        break;
                }

                let obj = {
                    'tempid': index,
                    'id': '',
                    'classRoomId': data.classRoomId,
                    'classTime': d + '',
                    'classTimeName': classTimeName,
                    // 'classTimeName': d.name,
                    'classRoom': data.classRoomName,
                    'startTime': data.classStartTime,
                    'endTime': data.classEndTime,
                    'action': ''
                };
                this.dataListSchedule.push(obj);
                first_index++;
            }
            this.bathAddClassRoomScheduleVisible = false;
            this.bathAddClassRoomData = {
                classTime: []
            };
        },
        bathAddClassOnChange: function (event) {
            console.log(this.bathAddClassRoomData.classTime);
        },
        whenBathClassStartTimeChange: function (event) {
            this.bathAddClassRoomData.classStartTime = event;
        },
        whenBathClassEndTimeChange: function (event) {
            this.bathAddClassRoomData.classEndTime = event;
        },
        whenSetClassRoom: function () {
            let flag = this.classRoomEntry;
            if (flag == 1) {
                var index = this.currentDataListScheduleIndex;
                if (index || index == 0) {
                    var obj = this.dataListSchedule[index];// 当前课表焦点
                    obj.classRoomId = this.$refs.classroomComp.classRoomSelected.id;
                    obj.classRoom = this.$refs.classroomComp.classRoomSelected.name;
                }
            } else if (flag = 2) {
                this.bathAddClassRoomData.classRoomName = this.$refs.classroomComp.classRoomSelected.name;
                this.bathAddClassRoomData.classRoomId = this.$refs.classroomComp.classRoomSelected.id;
            }
        },
    }
};
